package com.ludomancy.spaceopera.entities;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.util.Vector;

import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.ImageManipulation;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.entity.EntityCollisionFilter;
import com.ludomancy.spaceopera.BasicEntity;
import com.ludomancy.spaceopera.Game;
import com.ludomancy.spaceopera.Player;

public class Leviathan extends BasicEntity
{
	public static final int SPEED = 10;
	private Accumulator thruster = new Accumulator(Game.forTime(1000));
	private Accumulator hitter = new Accumulator(Game.forTime(50));
	private Image img;
	
	public Leviathan(Point pos)
	{
		super(pos);
		img = ResourceManager.getImage("leviathan.png");
		width = (int) proj.screenToWorldX(img.getWidth(null));
		height = (int) proj.screenToWorldY(img.getHeight(null));
	}
	
	public void paint(Graphics g, int x, int y)
	{
		g.drawImage(img, x, y, null);
	}
	
	public int[] getBitmask(int x, int y, int w, int h)
	{
		return ImageManipulation.extractBitmask(img, x, y, w, h);
	}
	
	public void onPlayerHit(Player player)
	{
		hitter.update();
		if(hitter.isDone())
		{
			player.hit(1);
			hitter.reset();
		}
	}
	
	public boolean update()
	{
		thruster.update();
		if(thruster.isDone())
		{
			pos.x -= SPEED;
			thruster.reset();
		}
		
		Vector near = parent.getEntities(new EntityCollisionFilter(this, proj));
		for (int i = 0; i < near.size(); i++)
		{
			BasicEntity e = (BasicEntity) near.elementAt(i);
			if(e instanceof Star)
			{
				// kill it
				((Star)e).consume();
			}
		}

		return false;
	}
	
}
